<template>
    <div id="app" :style="appStyle">
        <router-view></router-view>
    </div>
</template>

<script>
    import {ipcRenderer} from 'electron';
    import fs from 'fs';
    import path from 'path';
    import {loadDb} from '@/api/NEDB/nedbApi'

    export default {
        name: 'MyTools',
        data() {
            return {
                // 确保如果页面大小低于   1366 * 768  ，那么将页面宽度最低设置为1366 ，页面高度最低设置为768
                appStyle: {
                    width: 0 + 'px',
                    height: 0 + 'px'
                },

                isAppPathGot: false,
            }
        },
        created() {
            this.resetSize();
        },
        methods: {
            resetSize: function () {
                const cWidth = document.documentElement.clientWidth;
                const cHeight = document.documentElement.clientHeight;
                const mixWidth = this.$store.getters.mixWidth;
                const mixHeight = this.$store.getters.mixHeight;

                this.appStyle.width = (cWidth >= mixWidth ? cWidth : mixWidth) + 'px';
                this.appStyle.height = (cHeight >= mixHeight ? cHeight : mixHeight) + 'px';

                let that = this;
                if (that && !that._isDestroyed) {
                    setTimeout(() => {
                        if (that && !that._isDestroyed) {
                            that.resetSize();
                        }
                    }, 100);
                }
            },
            mkdirSync(dirname) {
                if (fs.existsSync(dirname)) {
                    return true;
                } else {
                    if (this.mkdirSync(path.dirname(dirname))) {
                        fs.mkdirSync(dirname);
                        return true;
                    }
                }
                return false
            },
            setPath(appPath){
                console.log('======初始化程序运行目录======');
                // 初始化，设置路径
                let path = appPath.replace(/\\/g,"/");

                let base_path = path;
                let data_path = path + '/../data';
                let temp_path = path + '/../temp';

                this.$store.dispatch('setBasePath', base_path);
                this.$store.dispatch('setDataPath', data_path);
                this.$store.dispatch('setTempPath', temp_path);

                this.mkdirSync(base_path);
                this.mkdirSync(data_path);
                this.mkdirSync(temp_path);

                this.isAppPathGot = true;
                this.initDB();
            },
            initDB(){
                console.log('======初始化数据库======');
                loadDb(this.$store.getters.dataPath);
            }
        },
        mounted() {
            const that = this;

            // 注册方法，用于从主线程获取程序目录后，获取返回值，并回调函数
            ipcRenderer.on('InitAppPath', function (event, arg) {
                that.setPath(arg);
            });

            // 启动程序
            console.log('======启动======');

            // 1、获取程序运行目录
            ipcRenderer.send('getAppPath');
            // 2、获取到运行目录后，执行数据库初始化任务
        }
    }
</script>

<style>
    /* CSS */
</style>
